package com.hongyuan.sellfitness.ui.find.view_mode;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.find.beans.FindChildBean;
import com.hongyuan.sellfitness.ui.find.beans.FindChildTopBean;
import com.hongyuan.sellfitness.ui.person.activity.AttentionActivity;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FindChildViewModel extends CustomViewModel<MyRepository> {

    public FindChildViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<FindChildTopItemViewModel> topList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FindChildTopItemViewModel> topBinding = ItemBinding.of(BR.viewModel, R.layout.item_find_child_top);

    //给RecyclerView添加ObservableList
    public ObservableList<FindChildItemViewModel> contentList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<FindChildItemViewModel> contentBinding = ItemBinding.of(BR.viewModel, R.layout.item_find_child);

    //选择位置
    public BindingCommand goFriends = new BindingCommand(() -> {
        startActivity(AttentionActivity.class);
    });


    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_MY_FRIENDS){
            List<FindChildTopBean.ListBean> mList = ((FindChildTopBean)dataBean).getList();

            if(mList != null && mList.size() > 0){
                topList.clear();
                for(int i = 0 ; i < mList.size() ; i++){
                    FindChildTopItemViewModel viewModel = new FindChildTopItemViewModel(this,mList.get(i),i,mList.size() - 1);
                    topList.add(viewModel);
                }
            }else{
                topList.clear();
            }
        }

        if(code == Constants.GET_CIRCLE_LIST){
            List<FindChildBean.ListBean> mList = ((FindChildBean)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                contentList.clear();
            }

            if(mList != null && mList.size() > 0){
                for(FindChildBean.ListBean bean : mList){
                    FindChildItemViewModel itemViewModel = new FindChildItemViewModel(this,bean);
                    contentList.add(itemViewModel);
                }
            }

            if(contentList.size() > 0){
                if(mList == null || mList.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }

        if(code == Constants.ADD_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(1);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() + 1);
                itemViewModel.entity.notifyChange();
            }
        }

        if(code == Constants.CANCEL_CIRCLE_PRAISE){
            if(itemViewModel != null){
                itemViewModel.entity.get().setIs_praise(0);
                itemViewModel.entity.get().setPraise_num(itemViewModel.entity.get().getPraise_num() - 1);
                itemViewModel.entity.notifyChange();
            }
        }
    }

    //操作的子项
    private FindChildItemViewModel itemViewModel;

    @Override
    public void itemClick(ItemViewModel itemVM) {
        super.itemClick(itemVM);

        itemViewModel = (FindChildItemViewModel) itemVM;

    }
}
